<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>     
<c:set var="ctx" value="${pageContext.request.contextPath}"/> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../include.jsp"/>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		dataGrid = $('#dg').treegrid({ 
			title:"资源列表",
			url:'${ctx}/module/listModules',   
		    idField:'moduleId',    
		    treeField:'moduleName', 
		    fit:true,
			fitColumns:true,
			striped:true,
			lines:true,
		    columns:[[    
		        {title:'资源名称',field:'moduleName',width:80},   
		        {title:'资源编码',field:'moduleCode',width:65},
		        {title:'url',field:'url',width:85},
		        {title:'资源类型',field:'moduleType',width:35,formatter:formatterType},    
		        {title:'排序',field:'sort',width:23},
		        {title:'图标',field:'iconCls',width:30},
		        {title:'状态',field:'activate',width:20,formatter:formatterFlag}
		    ]],
		    toolbar : '#toolbar',
		    onContextMenu: onContextMenu
		});  

		
	});
	function formatterFlag(value, row, index){
		if(value=="0"){
			return "停用";
		}else if(value =="1"){
			return "启用";
		}else{
			return value;
		}
	}
	function add(pId) {
		var url = "${ctx}/module/toModuleAdd";
		if(typeof(pId) != "undefined"){
			url = '${ctx}/module/toModuleAdd?pId='+pId;
		}
		parent.$.modalDialog({
			title:'添加资源',
			iconCls:'icon-save',
			href:url,
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			buttons:[{
				text:'提交',
				iconCls:'icon-ok',
				handler:function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function() {
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if (r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}
	
	function doSearch(){
		var params=$('#queryForm').serializeObject();
		$('#dg').treegrid('load',params);  
	}
	
	function doClear(){
		$('#queryForm').form("clear");
	}
	
	function del(id){
		if (id == undefined) {
			var rows = dataGrid.treegrid('getSelections');
			id = rows[0].moduleId;
		} else {
			dataGrid.datagrid('unselectAll').treegrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前用户？', function(b){ if(b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.ajax({
	   				type: "POST",
	   				url: '${ctx}/module/deleteModule',
	   				dataType:"json",
	   				data:{
   						ids : id
   					},
	   				success: function(result){
	     				if(result.success == true) {
     						$.messager.show({
								title:'系统提示',
								msg:result.msg,
								timeout:2000,
								showType:'slide'
							});
							dataGrid.treegrid('load');
	     				} else {
	     					parent.$.messager.alert('提示', result.msg, 'error');
	     				}    				
	   				}  				
				});
				parent.$.messager.progress('close');	
			} 
		});
	}
	
	
	function batchDel(){
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(b){ if(b){										
					$.each(rows,function(n,row) {			           
			            ids.push(row.moduleId);
			        });					
					$.ajax({
	   					type: "POST",
	   					url: '${ctx}/module/deleteModule',
	   					dataType:"json",
	   					data:{
	   						ids : ids.join(',')
	   					},
	   					success: function(result) {	   						
	     					if(result.success == true) {	     						
								parent.$.messager.alert('提示', result.msg, 'info');								
								dataGrid.datagrid('uncheckAll').datagrid('unselectAll').treegrid('clearSelections');
								dataGrid.treegrid('load');
	     					} else {
	     						parent.$.messager.alert('错误', result.msg, 'error');		     						
	     					}	     				
		   				}
					});
					parent.$.messager.progress('close');	
				}
			});
		} else {
			parent.$.messager.alert('系统提示', '请选择一条记录！', 'warning');
		}
	}
	
	
	function udpate(id){
		if (typeof(id) == 'undefined') {
			var rows = dataGrid.datagrid('getSelections');				
			if(rows.length != 1){
				parent.$.messager.alert('系统提示', '请选择一条记录！', 'warning');
				return;
			}
			id = rows[0].moduleId;	
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title:'编辑系统配置',
			iconCls:'icon-edit',
			href:'${ctx}/module/toModuleUpdate?moduleId='+id,
			width:'60%',
			height:'90%',
			resizable:true,
			collapsible:true,
			maximizable:true,
			buttons:[{
				text:'修改',
				iconCls:'icon-ok',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					parent.$.messager.confirm('系统提示', '确定要关闭当前窗口?', function(r){
		                if(r){
		                	parent.$.modalDialog.handler.dialog('close');
		                }
		            });					
				}
			}]
		});
	}
	
	function formatterType(value, row, index){
		if(row.moduleType ==1){
			return "url链接";
		}if(row.moduleType ==2){
			return "按钮权限码";
		}if(row.moduleType ==3){
			return "标签 accordion";
		}if(row.moduleType ==4){
			return "";
		}  
		return value;
	}
	
	function onContextMenu(e,row){
		if (row){
			e.preventDefault();
			$(this).treegrid('select', row.id);
			$('#mm').menu('show',{
				left: e.pageX,
				top: e.pageY
			});				
		}
	}
	
	function expand(){
		var node = $('#dg').treegrid('getSelected');
		if (node){
			$('#dg').treegrid('expand', node.id);
		}
	}
	function collapse(){
		var node = $('#dg').treegrid('getSelected');
		if (node){
			$('#dg').treegrid('collapse', node.id);
		}
	}
	
	function expandAll(){
		var node = $('#dg').treegrid('getSelected');
		if (node){
			$('#dg').treegrid('expandAll', node.id);
		}
	}
	function collapseAll(){
		var node = $('#dg').treegrid('getSelected');
		if (node){
			$('#dg').treegrid('collapseAll', node.id);
		}
	}
	function removeIt(){
		batchDel();
	}
	function append(){
		var node = $('#dg').treegrid('getSelected');
		if(node){
			add(node.id);
		}
	}
	
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'center',border:false">
		     <table id="dg"></table>
		 </div>
	</div>
	
	<div id="toolbar" style="display: none;">
		<a onclick="add();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
		<a onclick="udpate();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
		<a onclick="batchDel();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
	</div>
	<div id="mm" class="easyui-menu" style="width:120px;">
		<div onclick="append()" data-options="iconCls:'icon-add'">添加</div>
		<div onclick="udpate()" data-options="iconCls:'icon-edit'">修改</div>
		<div onclick="removeIt()" data-options="iconCls:'icon-remove'">删除</div>
		<div class="menu-sep"></div>
		<div onclick="collapseAll()" data-options="iconCls:'icon-standard-arrow-in'">折叠所有</div>
		<div onclick="expandAll()" data-options="iconCls:'icon-standard-arrow-out'">展开所有</div>
		<div onclick="expand()" data-options="iconCls:'icon-metro-expand2'">展开当前</div>
		<div onclick="collapse()" data-options="iconCls:'icon-metro-contract2'">折叠当前</div>	
	</div>
</body>
</html>